package cn.zzszxyy.hr.controller;

import cn.zzszxyy.hr.comm.ConvertToTree;
import cn.zzszxyy.hr.comm.Global;
import cn.zzszxyy.hr.comm.StringUtil;
import cn.zzszxyy.hr.model.MenuEntity;
import cn.zzszxyy.hr.service.MenuService;
import cn.zzszxyy.hr.support.AuthPassport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者:张伟
 * <p>
 * 联系:adamzzww@163.com
 * 时间:2016-11-30
 * 说明:无
 */
@Controller
@RequestMapping(Global.PATH)
public class MenuController extends ControllerEx {

    private final String path = "/menu";

    @Autowired
    private MenuService menuService;

    @AuthPassport
    @RequestMapping(value = path + "/list", method = RequestMethod.GET)
    public String get(HttpServletRequest request, ExtendedModelMap map) {
        Integer pageIndex = getPageIndex(request);
        Page<MenuEntity> page = menuService.findAllWithPage(pageIndex, Global.PAGE_SIZE);
        map.put("page", page);
        map.put("pageIndex", pageIndex);
        map.put("menuList", menuService.findAll());
        return Global.DESKTOP + path + "/menu-list";
    }

    @RequestMapping(value = path + "/{id}", method = RequestMethod.GET)
    public String get(@PathVariable int id, ExtendedModelMap map) {
        if (id >= 1) {
            MenuEntity menuEntity = menuService.findOne(id);
            map.put("menuEntity", menuEntity);
        }
        List<MenuEntity> menuList = menuService.findAll();
        map.put("menuList", menuList);
        return Global.DESKTOP + path + "/menu-info";
    }

    @RequestMapping(value = path + "/add", method = RequestMethod.GET)
    public String add(ExtendedModelMap map) {
        List<MenuEntity> menuList = menuService.findAll();
        map.put("menuList", menuList);
        return Global.DESKTOP + path + "/menu-info";
    }

    @ResponseBody
    @RequestMapping(value = path, method = {RequestMethod.PUT, RequestMethod.POST})
    public String post(MenuEntity menuEntity) {
        if (menuService.save(menuEntity) != null) {
            return Global.RE_SUCCESS;
        } else {
            return Global.RE_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping(value = path + "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable int id) {
        menuService.delete(id);
        return Global.RE_SUCCESS;//"redirect:" + Global.Web.PATH + path;
    }

    @ResponseBody
    @RequestMapping(value = path + "/tree", method = RequestMethod.GET)
    public List getTree(HttpServletRequest request) {
        String roleId = request.getParameter("roleId");
        List<MenuEntity> allMenu = menuService.findAll();
        List<String> menuList = new ArrayList<>();
        if (StringUtil.isEmpty(roleId)) {

        } else {
            List<MenuEntity> curMenu = menuService.findMenusByRoleId(Integer.parseInt(roleId));
            for (MenuEntity entity : curMenu) {
                menuList.add(entity.getMenuId().toString());
            }
        }
        return ConvertToTree.getMenuTree(allMenu, menuList);
    }

    @RequestMapping(value = path + "/treeframe", method = RequestMethod.GET)
    public String getTreeIframe(HttpServletRequest request, ExtendedModelMap map) {
        String roleId = request.getParameter("roleId");
        map.put("roleId", roleId);
        return Global.DESKTOP + path + "/menu-tree";
    }
}
